package com.proj.controller;

import com.proj.mapper.UserMapper;
import com.proj.pojo.User;
import com.proj.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.jws.WebParam;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @RequestMapping("/tolist")
    public String tolist(Model model){
        System.out.println("-------tolist");

        List<User> list = userService.getUserList();
        System.out.println( "list.size() : " + list.size());

        model.addAttribute("userList",list);

        return "userlist";
    }


    @RequestMapping("/toadd")
    public String toadd(){
        System.err.println("--------toadd");
        return "adduser";
    }

    @RequestMapping("/doAdduser")
    public String doAdduser(User user){
        System.err.println("---------doadd");
        System.out.println("username: "+user.getUserName());
        boolean res =userService.addUser(user);
        if (res){//新增成功
            System.out.println("---------添加成功");
            return "redirect:/user/tolist";
        }else{
            return "adduser";
        }
    }

    @RequestMapping("/toUpdateUser")
    public String toUpdateUser(@RequestParam String uid, Model model){
        System.out.println("uid: "+uid);
        //去后台查询
        //获取单个user 对象
        User user = new User();
        user.setId(1);
        user.setUserCode("bdqn");
        user.setUserName("测试账号");

        //将查询结果,封装到model 并传递到updateuser.jsp
        model.addAttribute("user",user);

        return  "updateuser";//jsp
    }

    @ResponseBody
    @RequestMapping("/doUpdateUser")
    public String doUpdateUser(){
        return "修改成功";
    }
}
